package com.xuyh.logonline.config;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import com.xuyh.logonline.filter.LogOnlineFilter;
import com.xuyh.logonline.properties.LogOnlineProperties;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.PostConstruct;

/**
 * @author xuyh
 * @date 2019年1月8日 20:28:08
 * @description 日志配置
 */
@Slf4j
@Configuration
public class LogbackConfig {

    @Autowired
    private LogOnlineWebSocket logOnlineWebSocket;
    @Autowired
    private LogOnlineProperties logOnlineProperties;


    @PostConstruct
    public void initLogback() {
        try {
            LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);
            context.addTurboFilter(new LogOnlineFilter(logOnlineWebSocket, logOnlineProperties));
            log.info("实时日志展示配置完成~");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("实时日志展示配置失败~");
        }
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
